package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.InfIndustry;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.BIndustry;
import cn.eonshine.cuba.lsms.modular.basic.service.IBIndustryClientService;
import cn.eonshine.cuba.lsms.modular.basic.service.IBIndustryService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

@Service
public class BIndustryClientService extends CommonService<InfIndustry> implements IBIndustryClientService{
	
	@Autowired
	private IBIndustryService iBIndustryService;
	
	private final String listUrl = "/infIndustry/list";
	private final String updateUrl = "/infIndustry/update";
	
	
	@Override
	public void requestIndustry() {
		this.requestData(listUrl, updateUrl);
	}


	@Override
	protected Object dataHandle(InfIndustry infIndustry) throws Exception {
		QueryWrapper<BIndustry> entity = new QueryWrapper<BIndustry>();
		entity.eq("ID", infIndustry.getCode());
		BIndustry info = iBIndustryService.getOne(entity);
		boolean isInsert = false;
		if(info == null) {
			info = new BIndustry();
			isInsert = true;
		}
		
		BeanUtil.copyProperties(infIndustry, info,true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
		Date nowTime = DateUtils.getCubaDate(Calendar.getInstance().getTime());
		info.setCreateBy(InfStatus.CREATE_BY);
		if(info.getCreateDate() == null) {
			info.setCreateDate(nowTime);
		}
		info.setUpdateBy(InfStatus.UPDATE_BY);
		if(info.getUpdateDate() == null) {
			info.setUpdateDate(nowTime);
		}
		
		if(isInsert) {
			iBIndustryService.save(info);
		}else {
			iBIndustryService.updateById(info);
		}
		
		return info;
	}


	@Override
	protected List<InfIndustry> feadBackDataHandle(Object obj, InfIndustry infIndustry, List<InfIndustry> feadBackDataLists) throws Exception {
		BIndustry info = (BIndustry) obj;
		InfIndustry tmpBack = new InfIndustry();
		tmpBack.setId(info.getId());
		tmpBack.setCode(infIndustry.getCode());
		tmpBack.setShortName(StringUtils.trim(infIndustry.getShortName()));
		tmpBack.setDataInputDatetime(infIndustry.getDataInputDatetime());
		tmpBack.setLsmsUpdate(BigDecimal.valueOf(InfStatus.LSMS_UPDATE_EXTRACT_YES));
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}


}
